package com.study.java.web.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.study.java.domain.User;
import com.study.java.service.UserService;
import com.study.java.service.impl.UserServiceImpl;
import com.study.java.utils.WebUtils;
import com.study.java.web.bean.UserFormBean;

/**
* @Name: ControllerServlet
* @Description: 用户操作控制器
* @Author: XXX
* @CreateDate: XXX
* @Version: V1.0
 */
public class ControllerServlet extends HttpServlet {
	
	private UserService userService = new UserServiceImpl() ;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String operate = request.getParameter("op") ;
		if("logout".equals(operate)) {
			logout(request, response) ;
		}
		if("login".equals(operate)) {
			login(request, response) ;		
		}
		if("register".equals(operate)) {
			register(request, response) ;
		}
	}
	
	/**
	* @Name: register
	* @Description: 注册
	* @Author: XXX
	* @Version: V1.0
	* @CreateDate: XXX
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Return: void
	 */
	private void register(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8") ;
		PrintWriter out = response.getWriter() ;
		//获取表单Bean实例对象
		UserFormBean userFormBean = WebUtils.fillBean(request, UserFormBean.class) ;
		//对页面表单中输入的字段格式进行校验
		if(!userFormBean.validate()) {
			request.setAttribute("userFormBean", userFormBean) ;
			request.getRequestDispatcher("/jsp/register.jsp").forward(request, response) ;
			return ;
		} 
		//对象拷贝：UserFormBean -> UserBean
		User user = WebUtils.copyProperties(User.class, userFormBean) ;
		//注册，保存到xml
		boolean result = userService.register(user) ;
		if(result) {
			//将用户信息保存到session
			HttpSession session = request.getSession() ;
			session.setAttribute("user", user) ;
			out.write("注册成功，2秒后跳转到系统首页！！！") ;
			response.setHeader("Refresh", "2;URL=" + request.getContextPath()) ;
		} else {
			userFormBean.getErrorMsg().put("username", "用户名已存在！") ;
			request.setAttribute("userFormBean", userFormBean) ;
			request.getRequestDispatcher("/jsp/register.jsp").forward(request, response) ;
		}
	}

	/**
	* @Name: login
	* @Description: 登录
	* @Author: XXX
	* @Version: V1.0
	* @CreateDate: XXX
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Return: void
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8") ;
		PrintWriter out = response.getWriter() ;
		//获取页面请求参数
		request.setCharacterEncoding("UTF-8") ; //设置请求参数内容编码，适用于post提交
		String username = request.getParameter("username") ;
		String password = request.getParameter("password") ;
		User user = userService.login(username, password) ;
		if(user != null) {
			HttpSession session = request.getSession() ;
			session.setAttribute("user", user) ;
			out.write("登录成功，2秒后跳转到系统首页！！！") ;
			response.setHeader("Refresh", "2;URL=" + request.getContextPath()) ;
		} else {
			out.write("用户名或密码错误，登录失败，请重新登录！！！") ;
			response.setHeader("Refresh", "2;URL=" + request.getContextPath() + "/jsp/login.jsp") ;
		}
	}

	/**
	* @Name: logout
	* @Description: 注销
	* @Author: XXX
	* @Version: V1.0
	* @CreateDate: XXX
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Return: void
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8") ;
		PrintWriter out = response.getWriter() ;
		HttpSession session = request.getSession() ;
		session.removeAttribute("user") ;
		out.write("注销成功，2秒后跳转到系统首页！！！") ;
		response.setHeader("Refresh", "2;URL=" + request.getContextPath()) ;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
